package com.app.newiptv.utils;

import com.app.newiptv.activity.Contast;
import com.app.newiptv.bean.RequestDto;
import com.app.newiptv.callback.IRequestLisenter;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Created by wendal on 2017/12/10.
 */

public class SendMacIp {

    public static void loadData(String url,String username) {

        OkGoUtil<String> okGoUtil = new OkGoUtil<String>(String.class, OkGoUtil.TYPE_STRING, new IRequestLisenter() {
            @Override
            public void onSuccessData(RequestDto dto) {

            }
            @Override
            public void onErrer(int code, String errMsg) {

            }
        });
        //okGoUtil.requestGet(url);
        APPLog.printInfo("name:" +username);
        okGoUtil.params("username",username);
        try {
            okGoUtil.params("ip",getLocalIPAddress());
            okGoUtil.params("mac",getLocalIPAddress());
        } catch (SocketException e) {
            e.printStackTrace();
        }
        okGoUtil.requestPost(url);
    }

    private static String getLocalIPAddress() throws SocketException {
        for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
            NetworkInterface intf = en.nextElement();
            for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
                InetAddress inetAddress = enumIpAddr.nextElement();
                if(!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)){
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
        return "null";
    }
    private static String getLocalEthernetMacAddress() {
        String mac=null;
        try {
            Enumeration localEnumeration= NetworkInterface.getNetworkInterfaces();
            while (localEnumeration.hasMoreElements()) {
                NetworkInterface localNetworkInterface=(NetworkInterface) localEnumeration.nextElement();
                String interfaceName=localNetworkInterface.getDisplayName();
                if (interfaceName==null) {
                    continue;
                }
                if (interfaceName.equals("eth0")) {
                    mac=convertToMac(localNetworkInterface.getHardwareAddress());
                    if (mac!=null&&mac.startsWith("0:")) {
                        mac="0"+mac;
                    }
                    break;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return mac;
    }
    private static String convertToMac(byte[] mac) {
        StringBuilder sb=new StringBuilder();
        for (int i=0; i<mac.length; i++) {
            byte b=mac[i];
            int value=0;
            if (b>=0&&b<=16) {
                value=b;
                sb.append("0"+Integer.toHexString(value));
            } else if (b>16) {
                value=b;
                sb.append(Integer.toHexString(value));
            } else {
                value=256+b;
                sb.append(Integer.toHexString(value));
            }
            if (i!=mac.length-1) {
                sb.append(":");
            }
        }
        return sb.toString();
    }
}
